package org.scratchcrew.web.controller;

import java.util.HashMap;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.util.ajax.JSON;
import org.scratchcrew.domain.Board;
import org.scratchcrew.services.BoardService;
import org.scratchcrew.web.jsf.ContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller(value = "boardController")
@Scope(value = "request")
public class BoardController {

	private static final int DEFAULT_BOARD_HEIGHT = 500;
	private static final int DEFAULT_BOARD_WIDTH = 1000;
	private static final String NEW_BOARD_DATA;
	static {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("height", DEFAULT_BOARD_HEIGHT);
		map.put("width", DEFAULT_BOARD_WIDTH);
		NEW_BOARD_DATA = JSON.toString(map);
	}
	private static final String DEFAULT_COMETD_SERVLET_MAPPING = "cometd";
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private BoardService boardService;

	private String cometdUrl;

	private Long boardId;

	private String cometdServletMapping = DEFAULT_COMETD_SERVLET_MAPPING;
	private boolean showBoard;

	@PostConstruct
	public void init() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		ServletContext context = (ServletContext) externalContext.getContext();
		HttpServletRequest request = (HttpServletRequest) externalContext
				.getRequest();
		String contextAddress = ContextUtils.contextAddress(context, request);
		this.cometdUrl = String.format("%s%s", contextAddress,
				cometdServletMapping);
	}

	public String getCometdUrl() {
		return cometdUrl;
	}

	public void setCometdUrl(String commetdUrl) {
		this.cometdUrl = commetdUrl;
	}

	public String getCometdServletMapping() {
		return cometdServletMapping;
	}

	public void setCometdServletMapping(String cometdServletMapping) {
		this.cometdServletMapping = cometdServletMapping;
	}

	public Long getBoardId() {
		return boardId;
	}

	public void setBoardId(Long boardId) {
		this.boardId = boardId;
	}

	public void changeBoard(ActionEvent event) {
		if (boardService.find(boardId) == null) {
			FacesMessage message = new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "board not found.", String
							.format("Cannot find board with id '%s'",
									this.boardId));
			FacesContext.getCurrentInstance().addMessage(null, message);
			showBoard = false;
		} else {
			showBoard = true;
		}
	}

	public void createBoard(ActionEvent event) {
		Board board = boardService.create(NEW_BOARD_DATA, null, null);
		this.boardId = board.getId();
		showBoard = true;
	}

	public boolean isShowBoard() {
		return showBoard;
	}

	public void setShowBoard(boolean showBoard) {
		this.showBoard = showBoard;
	}

}
